home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / GIFLIB12.ARJ / GIFINTER.C < prev    next >
C/C++ Source or Header  |  1991-05-12  |  10KB  |  269 lines

  1. /*****************************************************************************
  2. *   "Gif-Lib" - Yet another gif library.                     *
  3. *                                         *
  4. * Written by:  Gershon Elber                Ver 0.1, Jul. 1989   *
  5. ******************************************************************************
  6. * Program to flip interlaced and non interlaced images in GIF files.         *
  7. * Options:                                     *
  8. * -q : quite printing mode.                             *
  9. * -i : Force all images to be intelaced.                     *
  10. * -s : Force all images to be sequencial (non interlaced). This is default.  *
  11. * -h : on line help                                 *
  12. ******************************************************************************
  13. * History:                                     *
  14. * 5 Jul 89 - Version 1.0 by Gershon Elber.                     *
  15. * 21 Dec 89 - Fix problems with -i and -s flags (Version 1.1).               *
  16. *****************************************************************************/
  17.  
  18. #ifdef __MSDOS__
  19. #include <stdlib.h>
  20. #include <alloc.h>
  21. #endif /* __MSDOS__ */
  22.  
  23. #include <stdio.h>
  24. #include <ctype.h>
  25. #include <string.h>
  26. #include "gif_lib.h"
  27. #include "getarg.h"
  28.  
  29. #define PROGRAM_NAME    "GifInter"
  30.  
  31. #ifdef __MSDOS__
  32. extern unsigned int
  33.     _stklen = 16384;                 /* Increase default stack size. */
  34. #endif /* __MSDOS__ */
  35.  
  36. #ifdef SYSV
  37. static char *VersionStr =
  38.         "Gif library module,\t\tGershon Elber\n\
  39.     (C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  40. static char
  41.     *CtrlStr = "GifInter q%- i%- s%- h%- GifFile!*s";
  42. #else
  43. static char
  44.     *VersionStr =
  45.     PROGRAM_NAME
  46.     GIF_LIB_VERSION
  47.     "    Gershon Elber,    "
  48.     __DATE__ ",   " __TIME__ "\n"
  49.     "(C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  50. static char
  51.     *CtrlStr =
  52.     PROGRAM_NAME
  53.     " q%- i%- s%- h%- GifFile!*s";
  54. #endif /* SYSV */
  55.  
  56. /* Make some variables global, so we could access them faster: */
  57. static int
  58.     ImageNum = 0,
  59.     SequencialFlag = FALSE,
  60.     InterlacedFlag = FALSE,
  61.     HelpFlag = FALSE,
  62.     InterlacedOffset[] = { 0, 4, 2, 1 }, /* The way Interlaced image should. */
  63.     InterlacedJumps[] = { 8, 8, 4, 2 };    /* be read - offsets and jumps... */
  64.  
  65. static int LoadImage(GifFileType *GifFile, GifRowType **ImageBuffer);
  66. static int DumpImage(GifFileType *GifFile, GifRowType *ImageBuffer);
  67. static void QuitGifError(GifFileType *GifFileIn, GifFileType *GifFileOut);
  68.  
  69. /******************************************************************************
  70. * Interpret the command line and scan the given GIF file.              *
  71. ******************************************************************************/
  72. void main(int argc, char **argv)
  73. {
  74.     int    Error, NumFiles, ExtCode;
  75.     GifRecordType RecordType;
  76.     GifByteType *Extension;
  77.     char **FileName = NULL;
  78.     GifRowType *ImageBuffer;
  79.     GifFileType *GifFileIn = NULL, *GifFileOut = NULL;
  80.  
  81.     if ((Error = GAGetArgs(argc, argv, CtrlStr, &GifQuitePrint,
  82.         &InterlacedFlag, &SequencialFlag, &HelpFlag,
  83.         &NumFiles, &FileName)) != FALSE ||
  84.         (NumFiles > 1 && !HelpFlag)) {
  85.     if (Error)
  86.         GAPrintErrMsg(Error);
  87.     else if (NumFiles > 1)
  88.         GIF_MESSAGE("Error in command line parsing - one GIF file please.");
  89.     GAPrintHowTo(CtrlStr);
  90.     exit(1);
  91.     }
  92.  
  93.     if (HelpFlag) {
  94.     fprintf(stderr, VersionStr);
  95.     GAPrintHowTo(CtrlStr);
  96.     exit(0);
  97.     }
  98.  
  99.     if (NumFiles == 1) {
  100.     if ((GifFileIn = DGifOpenFileName(*FileName)) == NULL)
  101.         QuitGifError(GifFileIn, GifFileOut);
  102.     }
  103.     else {
  104.     /* Use the stdin instead: */
  105.     if ((GifFileIn = DGifOpenFileHandle(0)) == NULL)
  106.         QuitGifError(GifFileIn, GifFileOut);
  107.     }
  108.  
  109.     /* Open stdout for the output file: */
  110.     if ((GifFileOut = EGifOpenFileHandle(1)) == NULL)
  111.     QuitGifError(GifFileIn, GifFileOut);
  112.  
  113.     /* And dump out exactly same screen information: */
  114.     if (EGifPutScreenDesc(GifFileOut,
  115.     GifFileIn -> SWidth, GifFileIn -> SHeight,
  116.     GifFileIn -> SColorResolution, GifFileIn -> SBackGroundColor,
  117.     GifFileIn -> SBitsPerPixel, GifFileIn -> SColorMap) == GIF_ERROR)
  118.     QuitGifError(GifFileIn, GifFileOut);
  119.  
  120.     /* Scan the content of the GIF file and load the image(s) in: */
  121.     do {
  122.     if (DGifGetRecordType(GifFileIn, &RecordType) == GIF_ERROR)
  123.         QuitGifError(GifFileIn, GifFileOut);
  124.  
  125.     switch (RecordType) {
  126.         case IMAGE_DESC_RECORD_TYPE:
  127.         if (DGifGetImageDesc(GifFileIn) == GIF_ERROR)
  128.             QuitGifError(GifFileIn, GifFileOut);
  129.  
  130.         /* Put the image descriptor to out file: */
  131.         if (EGifPutImageDesc(GifFileOut,
  132.             GifFileIn -> ILeft, GifFileIn -> ITop,
  133.             GifFileIn -> IWidth, GifFileIn -> IHeight,
  134.             InterlacedFlag, GifFileIn -> IBitsPerPixel,
  135.             GifFileIn -> IColorMap) == GIF_ERROR)
  136.             QuitGifError(GifFileIn, GifFileOut);
  137.  
  138.         /* Load the image (either Interlaced or not), and dump it as */
  139.         /* defined in GifFileOut -> IInterlaced.             */
  140.         if (LoadImage(GifFileIn, &ImageBuffer) == GIF_ERROR)
  141.             QuitGifError(GifFileIn, GifFileOut);
  142.         if (DumpImage(GifFileOut, ImageBuffer) == GIF_ERROR)
  143.             QuitGifError(GifFileIn, GifFileOut);
  144.         break;
  145.         case EXTENSION_RECORD_TYPE:
  146.         /* Skip any extension blocks in file: */
  147.         if (DGifGetExtension(GifFileIn, &ExtCode, &Extension) == GIF_ERROR)
  148.             QuitGifError(GifFileIn, GifFileOut);
  149.         if (EGifPutExtension(GifFileOut, ExtCode, Extension[0],
  150.                             Extension) == GIF_ERROR)
  151.             QuitGifError(GifFileIn, GifFileOut);
  152.  
  153.         /* No support to more than one extension blocks, so discard: */
  154.         while (Extension != NULL) {
  155.             if (DGifGetExtensionNext(GifFileIn, &Extension) == GIF_ERROR)
  156.             QuitGifError(GifFileIn, GifFileOut);
  157.         }
  158.         break;
  159.         case TERMINATE_RECORD_TYPE:
  160.         break;
  161.         default:            /* Should be traps by DGifGetRecordType. */
  162.         break;
  163.     }
  164.     }
  165.     while (RecordType != TERMINATE_RECORD_TYPE);
  166.  
  167.     if (DGifCloseFile(GifFileIn) == GIF_ERROR)
  168.     QuitGifError(GifFileIn, GifFileOut);
  169.     if (EGifCloseFile(GifFileOut) == GIF_ERROR)
  170.     QuitGifError(GifFileIn, GifFileOut);
  171. }
  172.  
  173. /******************************************************************************
  174. * Routine to read Image out. The image can be Interlaced or None interlaced.  *
  175. * The memory required to hold the image is allocate by the routine itself.    *
  176. * The image is always loaded sequencially into the buffer.              *
  177. * Return GIF_OK if succesful, GIF_ERROR otherwise.                  *
  178. ******************************************************************************/
  179. static int LoadImage(GifFileType *GifFile, GifRowType **ImageBufferPtr)
  180. {
  181.     int Size, i, j, Count;
  182.     GifRowType *ImageBuffer;
  183.  
  184.     /* Allocate the image as vector of column of rows. We cannt allocate     */
  185.     /* the all screen at once, as this broken minded CPU can allocate up to  */
  186.     /* 64k at a time and our image can be bigger than that:             */
  187.     if ((ImageBuffer = (GifRowType *)
  188.     malloc(GifFile -> IHeight * sizeof(GifRowType *))) == NULL)
  189.         GIF_EXIT("Failed to allocate memory required, aborted.");
  190.  
  191.     Size = GifFile -> IWidth * sizeof(GifPixelType);/* One row size in bytes.*/
  192.     for (i = 0; i < GifFile -> IHeight; i++) {
  193.     /* Allocate the rows: */
  194.     if ((ImageBuffer[i] = (GifRowType) malloc(Size)) == NULL)
  195.         GIF_EXIT("Failed to allocate memory required, aborted.");
  196.     }
  197.  
  198.     *ImageBufferPtr = ImageBuffer;
  199.  
  200.     GifQprintf("\n%s: Image %d at (%d, %d) [%dx%d]:     ",
  201.     PROGRAM_NAME, ++ImageNum, GifFile -> ILeft, GifFile -> ITop,
  202.                  GifFile -> IWidth, GifFile -> IHeight);
  203.     if (GifFile -> IInterlace) {
  204.     /* Need to perform 4 passes on the images: */
  205.     for (Count = i = 0; i < 4; i++)
  206.         for (j = InterlacedOffset[i]; j < GifFile -> IHeight;
  207.                          j += InterlacedJumps[i]) {
  208.         GifQprintf("\b\b\b\b%-4d", Count++);
  209.         if (DGifGetLine(GifFile, ImageBuffer[j], GifFile -> IWidth)
  210.             == GIF_ERROR) return GIF_ERROR;
  211.         }
  212.     }
  213.     else {
  214.     for (i = 0; i < GifFile -> IHeight; i++) {
  215.         GifQprintf("\b\b\b\b%-4d", i);
  216.         if (DGifGetLine(GifFile, ImageBuffer[i], GifFile -> IWidth)
  217.         == GIF_ERROR) return GIF_ERROR;
  218.     }
  219.     }
  220.  
  221.     return GIF_OK;
  222. }
  223.  
  224. /******************************************************************************
  225. * Routine to dump image out. The given Image buffer should always hold the    *
  226. * image sequencially. Image will be dumped according to IInterlaced flag in   *
  227. * GifFile structure. Once dumped, the memory holding the image is freed.      *
  228. * Return GIF_OK if succesful, GIF_ERROR otherwise.                  *
  229. ******************************************************************************/
  230. static int DumpImage(GifFileType *GifFile, GifRowType *ImageBuffer)
  231. {
  232.     int i, j, Count;
  233.  
  234.     if (GifFile -> IInterlace) {
  235.     /* Need to perform 4 passes on the images: */
  236.     for (Count = GifFile -> IHeight, i = 0; i < 4; i++)
  237.         for (j = InterlacedOffset[i]; j < GifFile -> IHeight;
  238.                          j += InterlacedJumps[i]) {
  239.         GifQprintf("\b\b\b\b%-4d", Count--);
  240.         if (EGifPutLine(GifFile, ImageBuffer[j], GifFile -> IWidth)
  241.             == GIF_ERROR) return GIF_ERROR;
  242.         }
  243.     }
  244.     else {
  245.     for (Count = GifFile -> IHeight, i = 0; i < GifFile -> IHeight; i++) {
  246.         GifQprintf("\b\b\b\b%-4d", Count--);
  247.         if (EGifPutLine(GifFile, ImageBuffer[i], GifFile -> IWidth)
  248.         == GIF_ERROR) return GIF_ERROR;
  249.     }
  250.     }
  251.  
  252.     /* Free the m emory used for this image: */
  253.     for (i = 0; i < GifFile -> IHeight; i++) free((char *) ImageBuffer[i]);
  254.     free((char *) ImageBuffer);
  255.  
  256.     return GIF_OK;
  257. }
  258.  
  259. /******************************************************************************
  260. * Close both input and output file (if open), and exit.                  *
  261. ******************************************************************************/
  262. static void QuitGifError(GifFileType *GifFileIn, GifFileType *GifFileOut)
  263. {
  264.     PrintGifError();
  265.     if (GifFileIn != NULL) DGifCloseFile(GifFileIn);
  266.     if (GifFileOut != NULL) EGifCloseFile(GifFileOut);
  267.     exit(1);
  268. }
  269.